package day8;

import org.junit.Test;

public class StringDemo {

	@Test
	public void test1() {
		// "abc"直接双引号括起来的字符串叫常量字符串，
		// 如果常量池中有就直接使用，如果没有就在常量池中new一个字符串使用
		String str1	= "abc";
		// 自己手动new的字符串是放到堆里面
		String str2 = new String("abc");
		// 发现在常量池中有这个字符串"abc"，就直接执行常量池中已有的字符串
		String str3 = "abc";
		// 字符串==比较的是地址值
		System.out.println(str1 == str2);// false
		System.out.println(str2 == str3);// false
		System.out.println(str1 == str3);// true
	}
	
	@Test
	public void test2() {
		String str1 = new String("abc");
		String str2 = new String("Abc");
		System.out.println(str1 == str2);// false
		System.out.println(str1.equals(str2));// false
		System.out.println(str1.equalsIgnoreCase(str2));// true
	}
	
	@Test
	public void test3() {
		String str = "java AndroidA";
		System.out.println(str.length());
		char[] array = str.toCharArray();
		char ch = str.charAt(5);
		System.out.println(ch);// A
		
		System.out.println(str.indexOf('A'));//5
		System.out.println(str.indexOf('X'));//-1
		System.out.println(str.indexOf('A', 6));//12
		System.out.println(str.indexOf("And"));//5
		System.out.println(str.lastIndexOf('A'));//12
	}
	
	@Test
	public void test4() {
		String str = "java AndroidA";
		System.out.println(str.contains("And"));// true
		System.out.println(str.startsWith("java"));//true
		// offset:偏移
		System.out.println(str.startsWith("And", 5));//true
		System.out.println(str.endsWith("oidA"));// true
		System.out.println(str.isEmpty());// false
		int[] array = new int[2];
		//array.length;
		System.out.println("".isEmpty());//true
	}
	
	@Test
	public void test5() {
		String str = "java And";
		char[] chs = str.toCharArray();
		// ArrayList、数组都是可以使用foreach遍历
		for (char c : chs) {
			System.out.println(c);
		}
		
		char[] array = {'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};
		String str2 = new String(array);
		System.out.println(str2);
	}
	
	@Test
	public void test6() {
		String str = "java AndroidA";
		String newStr = str.replace('A', 'B');
		System.out.println(str);
		System.out.println(newStr);
	}
	
	@Test
	public void test7() {
		String str = "java Android Python";
		String[] array = str.split(" ");
		for (String string : array) {
			System.out.println(string);
		}
	}
	
	@Test
	public void test8() {
		String str = "java AndroidA";
		System.out.println(str.substring(5));// AndroidA
		System.out.println(str.substring(5, 8));// And
	}
	
	@Test
	public void test9() {
		String str = "   java AndroidA   ";
		System.out.println(str);
		System.out.println(str.trim());
		
	}
	
	@Test
	public void test10() {
		String str1 = "abc";
		String str2 = new String("abc");
		StringBuffer stringBuffer = new StringBuffer("abc");
		StringBuilder stringBuilder = new StringBuilder("abc");
	}
	
	@Test
	public void test11() {
		String str1 = "Java";
		String str2 = "PHP";
		String str3 = "Python";
		String str4 = "C";
		// 因为String类型长度是不可改变的，每次(+)做拼接的时候
		// 都会new新的字符串对象
		String str = str1 + str2 + str3 + str4;
		StringBuilder builder = new StringBuilder(str1);// Java
		builder.append(str2);// JavaPHP
		builder.append(str3);// JavaPHPPython
		builder.append(str4);// JavaPHPPythonC
		System.out.println(builder);
		
		// 链式编程
		StringBuilder builder2 = new StringBuilder(str1);
		builder2.append(str2).append(str3).append(str4);
	}
	
	
	
	
}
